//
//  LGAboutViewController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/2/15.
//

#import "LGAboutViewController.h"
#import "LGGetWebViewController.h"
#import "LGLineTitleTipView.h"
@interface LGAboutViewController ()
/**   */
@property (nonatomic , strong)UIImageView *logoView;
/**   */
@property (nonatomic , strong)UILabel *titleLabel;
/**   */
@property (nonatomic , strong)UILabel *versionLabel;
/**   */
@property (nonatomic , strong)LGLineTitleTipView *updateView;
/**   */
@property (nonatomic , strong)LGLineTitleTipView *agreementView;
/**   */
@property (nonatomic , strong)LGLineTitleTipView *privacyView;
/**   */
@property (nonatomic , strong)UILabel *bottomLabel;
/**   */
@property (nonatomic , strong)NSDateFormatter *formatter;
/**   */
@property (nonatomic , copy)NSString *latestVersion;

@end

@implementation LGAboutViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"关于我们";
    [self.view addSubview:self.logoView];
    [self.view addSubview:self.titleLabel];
    [self.view addSubview:self.versionLabel];
    [self.view addSubview:self.updateView];
    [self.view addSubview:self.agreementView];
    [self.view addSubview:self.privacyView];
    [self.view addSubview:self.bottomLabel];
    [self.view addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(lineViewTouchAction:)]];
    [self requestData];
    
}

-(void)requestData{
    [RequestUtil withPOST:@"/doctor/my.about/version" parameters:@{@"client":@"ios",@"client_name":@"doctor"} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if(code == 200){
            self.latestVersion = LGNSString(returnData[@"version"]);
            self.updateView.content = [NSString stringWithFormat:@"v%@",returnData[@"version"]];
        }
    } error:^(NSError *error) {
        
    }];
}

-(void)lineViewTouchAction:(UITapGestureRecognizer *)sender{
    CGPoint point = [sender locationInView:self.view];
    if(CGRectContainsPoint(self.updateView.frame, point)){
        //检查更新
        [self needUpdateApp];
        
    }else if (CGRectContainsPoint(self.agreementView.frame, point)){
        //用户协议
        LGGetWebViewController *controller = [[LGGetWebViewController alloc]init];
        controller.urlStr = AgreementLink;
        controller.title = @"用户协议";
        [self.navigationController pushViewController:controller animated:YES];
        
    }else if (CGRectContainsPoint(self.privacyView.frame, point)){
        //隐私政策
        LGGetWebViewController *controller = [[LGGetWebViewController alloc]init];
        controller.urlStr = PrivacyLink;
        controller.title = @"隐私政策";
        [self.navigationController pushViewController:controller animated:YES];
    }
}

-(void)needUpdateApp{
    BOOL needUpdate = NO;
    NSString *currentVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
    NSArray *currenArray = [currentVersion componentsSeparatedByString:@"."];
    
    if(self.latestVersion.length>0){
        NSArray *newArray = [self.latestVersion componentsSeparatedByString:@"."];
        for (NSInteger i = 0; i < newArray.count; i++) {
            if ([currenArray[i] integerValue] < [newArray[i] integerValue]) {
                needUpdate = YES;
                break;
            }
        }
        if(needUpdate == YES){
            //跳转应用市场
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:AppLink] options:@{} completionHandler:nil];
            
        }else{
            [TooltipView showMessage:@"已是最新版本" offset:0];
        }
    }else{
        [self requestData];
    }
}

#pragma mark
#pragma mark ====> 创建控件
-(UIImageView *)logoView{
    if(!_logoView){
        _logoView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:AppIcon]];
        _logoView.frame = CGRectMake((Screen_W-viewPix(66))/2.0, viewPix(40), viewPix(66), viewPix(66));
        _logoView.cornerRidus = 10;
    }
    return _logoView;
}

-(UILabel *)titleLabel{
    if(!_titleLabel){
        _titleLabel = [UILabel lableWithText:AppTitle colorString:@"#333333" textFont:LGFontWeight(16, UIFontWeightMedium) textAlignment:NSTextAlignmentCenter lines:1];
        _titleLabel.frame = CGRectMake(viewPix(20), kMaxY(self.logoView.frame)+viewPix(16), Screen_W-viewPix(40), viewPix(20));
    }
    return _titleLabel;
}

-(UILabel *)versionLabel{
    if(!_versionLabel){
        NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
        _versionLabel = [UILabel lableWithText:[NSString stringWithFormat:@"v%@",version] colorString:@"#333333" textFont:LGFont(16) textAlignment:NSTextAlignmentCenter lines:1];
        _versionLabel.frame = CGRectMake(viewPix(20), kMaxY(self.titleLabel.frame)+viewPix(9), Screen_W-viewPix(40), viewPix(20));
    }
    return _versionLabel;
}

-(LGLineTitleTipView *)updateView{
    if(!_updateView){
        _updateView = [[LGLineTitleTipView alloc] initWithFrame:CGRectMake(viewPix(24), kMaxY(self.versionLabel.frame)+viewPix(65), Screen_W-viewPix(48), 60)];
        _updateView.title = @"检查更新";
        _updateView.type = @"text";
    }
    return _updateView;
}

-(LGLineTitleTipView *)agreementView{
    if(!_agreementView){
        _agreementView = [[LGLineTitleTipView alloc] initWithFrame:CGRectMake(viewPix(24), kMaxY(self.updateView.frame), Screen_W-viewPix(48), 60)];
        _agreementView.title = @"用户协议";
        _agreementView.type = @"text";
    }
    return _agreementView;
}

-(LGLineTitleTipView *)privacyView{
    if(!_privacyView){
        _privacyView = [[LGLineTitleTipView alloc] initWithFrame:CGRectMake(viewPix(24), kMaxY(self.agreementView.frame), Screen_W-viewPix(48), 60)];
        _privacyView.lineView.hidden = YES;
        _privacyView.title = @"隐私政策";
        _privacyView.type = @"text";
    }
    return _privacyView;
}


-(UILabel *)bottomLabel{
    if(!_bottomLabel){
        NSString *year = [self.formatter stringFromDate:[NSDate date]];
        _bottomLabel = [UILabel lableWithText:[NSString stringWithFormat:@"Copyright © 2022-%@ 杭州焰竹健康管理有限公司",year] colorString:@"#999999" textFont:LGFont(12) textAlignment:NSTextAlignmentCenter lines:2];
        _bottomLabel.frame = CGRectMake(viewPix(50), Screen_H-topBarHeight-viewPix(60)-bottomSafeBarHeight/2.0, Screen_W-viewPix(100), viewPix(40));
        [_bottomLabel lineSpacing:5];
    }
    return _bottomLabel;
}

-(NSDateFormatter *)formatter{
    if(!_formatter){
        _formatter = [[NSDateFormatter alloc] init];
        _formatter.dateFormat = @"YYYY";
    }
    return _formatter;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
